//
//  TrainSelectController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TrainSelectController.h"
#import "Train.h"
#import "TrainController.h"
#import "DataBaseHandler.h"
@interface TrainSelectController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *lineArr;
@property (nonatomic, strong) NSMutableArray *changeArr;
@property (nonatomic, strong) NSMutableArray *bigArr;
@property (nonatomic, strong) DataBaseHandler *dataBase;

@end

@implementation TrainSelectController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.lineArr = [NSMutableArray array];
        self.changeArr = [NSMutableArray array];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 打开数据库
    self.dataBase = [DataBaseHandler shareInstance];
    [self.dataBase openDB];
    // Do any additional setup after loading the view.
    // 创建tableview
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.bigArr objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"0502"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"0502"];
    }
    Train *train = [[self.bigArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 0) {
        cell.textLabel.text = train.line;
    }else {
        NSString *str = [NSString stringWithFormat:@"%@ --> %@", train.from, train.to];
        cell.textLabel.text = str;
    }
    return cell;
}
// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TrainController *trainVC = [[TrainController alloc] init];
    Train *train = [[self.bigArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 0) {
        trainVC.line = train.line;
    }else {
        trainVC.fromStation = train.from;
        trainVC.toStation = train.to;
    }
    [self.navigationController pushViewController:trainVC animated:YES];
//    [self presentViewController:trainVC animated:YES completion:^{
//        
//    }];
}
// 设置section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.bigArr.count;
}
// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
// 在section上放按钮
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    if (section == 0) {
        [button setTitle:@"车次" forState:UIControlStateNormal];
    }else {
        [button setTitle:@"站到站" forState:UIControlStateNormal];
    }
    [button addTarget:self action:@selector(targetButton:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor whiteColor];
    return button;
}
- (void)targetButton:(UIButton *)button
{
    if ([button.titleLabel.text isEqualToString:@"车次"]) {
        if (self.lineArr.count == 0) {
        self.lineArr = [self.dataBase selectTrainOfLine];
//        self.changeArr = nil;
//        self.changeArr = [NSMutableArray array];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, nil];
        }else {
            self.lineArr = [NSMutableArray array];
            self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, nil];
        }
        [self.tableView reloadData];
    }else {
        if (self.changeArr.count == 0) {
        self.changeArr = [self.dataBase selectTrainOfChange];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, nil];
        }else {
            self.changeArr = [NSMutableArray array];
            self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, nil];
        }
        [self.tableView reloadData];
    }
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        [self.dataBase deleteTrainOfLine:cell.textLabel.text];
        [self.lineArr removeObjectAtIndex:indexPath.row];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, nil];
        [self.tableView reloadData];
    }else {
        Train *train = [self.changeArr objectAtIndex:indexPath.row];
        [self.dataBase deleteTrainOfStation:train.combine1];
        [self.changeArr removeObjectAtIndex:indexPath.row];
        self.bigArr = [NSMutableArray arrayWithObjects:self.lineArr, self.changeArr, nil];
        [self.tableView reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
